package com.king.wms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.king.wms.pojo.Sku;
import com.king.wms.pojo.Storer;
import com.king.wms.service.SkuService;
import com.king.wms.service.StorerService;

@Controller
@RequestMapping(value = "/storer")
public class StorerController {

	@Autowired
	private StorerService storerService;
	
	@Autowired
	private SkuService skuService;

	@RequestMapping(value = "/insert")
	public String insert(Storer storer) {
		storerService.insert(storer);
		return "redirect:/storer/findAll";
	}

	@RequestMapping("/toUpdateStorerPage/{storer_id}")
	public void toUpdateStorerPage(@PathVariable("storer_id") Integer storer_id,
			HttpServletResponse response, HttpServletRequest request)
			throws ServletException, IOException {
		
		Storer storer = storerService.findStorerByStorerkey(storer_id);
		System.out.println("==========================="+storer);
		request.setAttribute("storer", storer);
		
		request.getRequestDispatcher("/jsp/storer/updateStorer.jsp").forward(
				request, response);

	}
	
	@RequestMapping(value = "/findSkuByStorer_id/{storer_id}")
	public void findSkuByStorer_id(@PathVariable("storer_id") Integer storer_id,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<Sku> list_sku = skuService.findSkuByStorer_id(storer_id);
		//Loc loc = locService.findLocByLoc(loc_id);
		
		request.setAttribute("storer_id", storer_id);
		request.setAttribute("list_sku", list_sku);
		request.getRequestDispatcher("/jsp/storer/storerSku.jsp").forward(request,
				response);
		
	}
	

	@RequestMapping(value = "/update")
	public String update(Storer storer) {
		storerService.update(storer);
		return "redirect:/storer/findAll";
	}

	@RequestMapping(value = "/delete/{storer_id}")
	public String delete(@PathVariable Integer storer_id) {
		storerService.delete(storer_id);
		return "redirect:/storer/findAll";
	}


	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<Storer> list_storer = storerService.findAll();
		System.out.println(list_storer);
		
		request.setAttribute("list_storer", list_storer);
		request.getRequestDispatcher("/jsp/storer/storer.jsp").forward(request,response);
	}

}
